package cn.hwadee.muying.controller;


import cn.hwadee.muying.commons.constant.ConstantRole;
import cn.hwadee.muying.service.ContractService;
import cn.hwadee.muying.service.WorkerService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import cn.hwadee.muying.commons.base.BaseController;
import cn.hwadee.muying.commons.jqgrid.GridResult;
import cn.hwadee.muying.commons.page.MyPage;
import org.springframework.ui.Model;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.hwadee.muying.service.ReturnVisitService;
import cn.hwadee.muying.entity.ReturnVisit;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.shiro.authz.annotation.Logical;

/**
* <p>
* 回访记录 前端控制器
*
* </p>
*
* @author Mankind
* @since 2018-08-06
*/
@Controller
@RequestMapping("/sys/returnVisit")
public class ReturnVisitController extends BaseController {
    @Autowired
    private ReturnVisitService returnVisitService;
    @Autowired
    private WorkerService workerService;
    @Autowired
    private ContractService contractService;
    @ModelAttribute("returnVisit")
    public ReturnVisit get(@RequestParam(required = false)Long id) throws Exception{
        if(id != null && id > 0){
            return returnVisitService.selectById(id);
        }else{
            return new ReturnVisit();
        }
    }

    @RequiresPermissions("sys:returnVisit:list")
    @RequestMapping(value="/list",produces = "text/html;charset=utf-8",method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        return "/biz/returnVisit/list";
    }

    @ResponseBody
    @RequiresPermissions("sys:returnVisit:list")
    @RequestMapping(value="/list.json",produces = "application/json;charset=utf-8")
    public GridResult list() throws Exception{
        MyPage<ReturnVisit> page = getPage();
        page = returnVisitService.list(page,getCurrentUser());
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    /**
     * 跳转添加/编辑回访记录界面
     * @param returnVisit
     * @param model
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:returnVisit:edit","sys:returnVisit:add"},logical=Logical.OR)
    @RequestMapping(value = {"/edit","/add"}, produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String add(@ModelAttribute ReturnVisit returnVisit,Long rid,Model model) throws Exception{
        if(rid==null){
            rid= ConstantRole.ROLE_NURSERY_TEACHER;
        }
        model.addAttribute("rid",rid);
        model.addAttribute("returnVisit",returnVisit);
        model.addAttribute("visitedPeoples",contractService.selectReturnVisitList(getCurrentUser().getOrgId(),rid));
        return "/biz/returnVisit/edit";
    }

    /**
     * 处理更新回访记录的请求
     * @param returnVisit
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:returnVisit:edit","sys:returnVisit:add"},logical=Logical.OR)
    @RequestMapping(value = "/edit", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute ReturnVisit returnVisit){
        Map<String,Object> result = new HashMap();
        try {
        if(returnVisit.getId() != null && returnVisit.getId() > 0){
            returnVisitService.updateById(returnVisit);
        }else {
            returnVisit.setCallerId(getCurrentUser().getId());
            returnVisit.setOrgId(getCurrentUser().getOrgId());
            returnVisit.setVisitDate(new Date());
            returnVisitService.insert(returnVisit);
        }
        result.put("status",200);
        result.put("msg","回访记录信息保存成功");
        }catch (Exception e){
        e.printStackTrace();
        result.put("status",500);
        result.put("msg","回访记录信息保存失败");
        }
        return result;
    }

    /**
     * 处理删除回访记录的请求
     * @param id
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:returnVisit:delete")
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashMap();
        try {
        if(id != null && id > 0){
            returnVisitService.deleteById(id);
            result.put("status",200);
            result.put("msg","删除回访记录成功");
        }else {
            result.put("status",500);
            result.put("msg","请选择删除的回访记录");
        }
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","删除回访记录失败");
        }
        return result;
    }

}
